package com.shangxia.dreaming.common.util.excelUtil.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: lucky
 * @ClassName: NoModelDataListener
 * @Time: 2020/9/5
 * @Author: zhouy
 * @Description:
 * @Experience:
 */

@Slf4j
public class NoModelDataListener extends AnalysisEventListener<Map<Integer, String>> {

    /**
     * 每隔5条存储数据库，实际使用中可以3000条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 100;
    List<Map<Integer, String>> list = new ArrayList<Map<Integer, String>>();
    public static int MAXSIZE = 0;
    public static List<List<String>> ALL_RESULT = new ArrayList<>();


    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
        list.add(data);
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();
        log.info("所有数据解析完成！");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {
        for (Map<Integer, String> map : list) {
            MAXSIZE = Math.max(MAXSIZE, map.values().size()/5);
            ALL_RESULT.add(new ArrayList<>(map.values()));
        }
    }
}
